import { getLogByPage, addLog, updateLog, delLog } from '@/services/system/log';
import { PlusOutlined } from '@ant-design/icons';
import { PageHeaderWrapper } from '@ant-design/pro-layout';
import type { ActionType, ProColumns } from '@ant-design/pro-table';
import ProTable from '@ant-design/pro-table';
import { Button, Form, Input, message, Modal, Tree } from 'antd';
import { sortTran, filterTran } from '@/utils/requestUtil';
import React, { useEffect, useRef, useState } from 'react';
const Log: React.FC<{}> = () => {
  const actionRef = useRef<ActionType>();
  const [modalVisit, setModalVisit] = useState(false); // 新建和编辑提示框
  const [confirmLoading, setConfirmLoading] = useState(false); //编辑框的disable
  const [formTitle, setFormTitle] = useState('新建'); //
  const [createOrUpdate, setCreateOrUpdate] = useState(false); //编辑框的disable
  const [form] = Form.useForm();
  const columns: ProColumns[] = [
    {
      title: '序号',
      dataIndex: 'index',
      valueType: 'index',
      align: 'center',
      sorter: true,
      width: 80,
    },
    {
      title: '方法',
      dataIndex: 'method',
      align: 'center',
      sorter: true,
      width: 250,
      ellipsis: true,
    },
    {
      title: '参数',
      dataIndex: 'params',
      align: 'center',
      sorter: true,
      width: 250,
      ellipsis: true,
    },
    {
      title: '地址',
      dataIndex: 'address',
      align: 'center',
      sorter: true,
    },
    {
      title: '浏览器',
      dataIndex: 'browser',
      align: 'center',
      sorter: true,
    },
    {
      title: '描述',
      dataIndex: 'description',
      align: 'center',
      sorter: true,
    },
    {
      title: '异常详情',
      dataIndex: 'exceptionDetail',
      align: 'center',
      sorter: true,
    },
    {
      title: '日志类型',
      dataIndex: 'logType',
      align: 'center',
      sorter: true,
    },

    {
      title: '请求Ip',
      dataIndex: 'requestIp',
      align: 'center',
      sorter: true,
    },
    {
      title: '请求时间',
      dataIndex: 'time',
      align: 'center',
      sorter: true,
    },
    {
      title: '用户名称',
      dataIndex: 'username',
      align: 'center',
      sorter: true,
    },
    {
      title: '创建日期', //表头显示的名称
      search: false,
      dataIndex: 'createTime',
      align: 'center',
      sorter: true,
    },
    {
      title: '创建人', //表头显示的名称
      search: false,
      dataIndex: 'createName',
      align: 'center',
      sorter: true,
    },
    {
      title: '操作',
      valueType: 'option',
      key: 'option',
      fixed: 'right',
      align: 'center',
      render: (text, record, _, action) => [
        <a
          key="editKey"
          onClick={() => {
            setModalVisit(true);
            setCreateOrUpdate(true);
            setFormTitle('编辑');
            form.setFieldsValue({
              address: record.address,
              browser: record.browser,
              description: record.description,
              exceptionDetail: record.exceptionDetail,
              logType: record.logType,
              method: record.method,
              params: record.params,
              requestIp: record.requestIp,
              time: record.time,
              username: record.username,
              id: record.id,
            });
          }}
        >
          编辑
        </a>,
        <a
          key="deleteKey"
          onClick={() => {
            Modal.confirm({
              title: '删除',
              content: '确定要删除吗',
              okText: '确认',
              cancelText: '取消',
              onOk: async () => {
                await delLog({ id: record.id });
                actionRef.current.reload();
              },
            });
          }}
        >
          删除
        </a>,
      ],
    },
  ];
  const handleSubmit = async () => {
    setConfirmLoading(true);
    if (formTitle == '新建') {
      const result = await addLog(form.getFieldsValue());
      if (result != null) {
        message.success('添加成功');
      }
    } else {
      await updateLog(form.getFieldsValue());
    }
    setConfirmLoading(false);
    setModalVisit(false);
    actionRef.current.reload();
  };

  return (
    <PageHeaderWrapper title={false} breadcrumb={undefined}>
      <ProTable
        headerTitle="操作日志查询"
        actionRef={actionRef} //用于触发刷新操作等，看api
        rowKey="id" //表格行 key 的取值，可以是字符串或一个函数
        toolBarRender={(action, { selectedRows }) => [
          <Button
            key="button"
            icon={<PlusOutlined />}
            onClick={() => {
              setCreateOrUpdate(false);
              setFormTitle('新建');
              form.resetFields();
              setModalVisit(true);
            }}
            type="primary"
          >
            新建
          </Button>,
        ]}
        request={async (params = {}, sort, filter) => {
          const sorters = sortTran(sort);
          const filters = filterTran(params);
          const result = await getLogByPage({ ...params, sorters: sorters, filters: filters });
          return {
            data: result.data.content,
            total: result.data.totalElements,
            success: result.success,
          };
        }}
        columns={columns}
        scroll={{ x: 2000, y: 350 }}
        pagination={{
          defaultPageSize: 100,
          defaultCurrent: 1,
          pageSizeOptions: [100, 500, 1000],
          showSizeChanger: true,
        }}
        rowSelection={{}}
      />
      <Modal
        title={formTitle}
        visible={modalVisit}
        onOk={handleSubmit}
        confirmLoading={confirmLoading}
        onCancel={() => setModalVisit(false)}
      >
        <Form
          name="wrap"
          labelCol={{ flex: '110px' }}
          labelAlign="left"
          labelWrap
          form={form}
          wrapperCol={{ flex: 1 }}
          colon={false}
        >
          <Form.Item label="id" name="id" hidden={true}>
            <Input />
          </Form.Item>
          <Form.Item
            label=""
            name="address"
            rules={[{ required: true, message: 'Please input your address!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
            label=""
            name="browser"
            rules={[{ required: true, message: 'Please input your browser!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
            label=""
            name="description"
            rules={[{ required: true, message: 'Please input your description!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
            label=""
            name="exceptionDetail"
            rules={[{ required: true, message: 'Please input your exceptionDetail!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
            label=""
            name="logType"
            rules={[{ required: true, message: 'Please input your logType!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
            label=""
            name="method"
            rules={[{ required: true, message: 'Please input your method!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
            label=""
            name="params"
            rules={[{ required: true, message: 'Please input your params!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
            label=""
            name="requestIp"
            rules={[{ required: true, message: 'Please input your requestIp!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
            label=""
            name="time"
            rules={[{ required: true, message: 'Please input your time!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
            label=""
            name="username"
            rules={[{ required: true, message: 'Please input your username!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
        </Form>
      </Modal>
    </PageHeaderWrapper>
  );
};
export default Log;
